import request from '@/utils/request'
import { IResp } from './interfaces'

export type DBKeys =
    | 'users'
    | 'articles'
    | 'bigModelConfigs'
    | 'siteConfigs'
    | 'teachCases'
    | 'knowledgeBases'
    | 'knowledgeBaseFiles'
    | 'articleComments'
    | 'teachCaseComments'
    | 'questions'
    | 'userAnswers'

export interface IDict {
    label: string
    value: string
}

// 获取字典
export const getDict = (db: DBKeys, labelProp = 'id', valueProp = 'id') => {
    return request.get<any, IDict[]>(`/dict/${db}`, {
        params: {
            labelProp,
            valueProp,
        },
    })
}

// 获取分页列表
export const getList = <T = any>(
    db: DBKeys,
    searchParams: any,
    dbInclude: any[] = []
) => {
    return request.post<any, IResp<T>>(`/crud/getList/${db}`, {
        page: 1,
        pageSize: 10,
        ...searchParams,
        dbInclude,
    })
}

// 删除
export const del = (db: DBKeys, id: number) => {
    return request.delete<any, IResp>(`/crud/${db}/${id}`)
}

// 更新
export const update = (db: DBKeys, id: number, data: any) => {
    return request.put<any, IResp>(`/crud/${db}/${id}`, data)
}

// 添加
export const add = (db: DBKeys, data: any) => {
    return request.post<any, IResp>(`/crud/${db}`, data)
}
